package com.atguigu.com.seckill.Interceptor;

import com.atguigu.com.seckill.vo.UserInfoVo;
import com.atguigu.common.constan.AuthServerConstant;
import com.atguigu.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 拦截器
 */
@Component
public class CatInterceptor implements HandlerInterceptor {
    public static ThreadLocal<UserInfoVo> threadlocal = new ThreadLocal<>();

    /**
     * 目标方法前进行拦截
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求信息
        HttpSession session = request.getSession();
        MemberVo attribute = (MemberVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
        UserInfoVo userInfoVo = new UserInfoVo();
        //判断是否为空
        if (attribute != null) {
            userInfoVo.setId(attribute.getId());
        }
        return false;
    }

}
